package org.jimmy.tang.lang;

import java.util.Random;

/**
 * @author jimmy
 * 
 */
public class ObjectDesign {
    public static void main(String[] args) {
        Employee1[] staff = new Employee1[2];
        staff[0] = new Employee1("Bob", 1000);
        staff[1] = new Employee1("Jim", 2000);

        System.out.println(staff[0].getBonus());// 可能是12000.0
        System.out.println(staff[1].getBonus());// 可能是13000.0
    }
}

class Employee1 {
    // 默认构造器
    public Employee1() {
        // name initialized to ""
        // salary initialized to 0.0
        // id initialized to 0
        // bonus initialized to 0.0
    }

    // 重载构造器
    public Employee1(String name) {
        this.name = name;
        id = nextId;
        nextId++;
    }

    public Employee1(String name, double salary) {
        this(name);// 调用另一构造器
        this.salary = salary;
    }

    // 定义访问器方法
    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public int getId() {
        return id;
    }

    public double getBonus() {
        return bonus;
    }

    // 定义更改器方法
    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    /**
     * Raise the salary of an employee
     * @param percent the pecentage by which to raise the salary(0.1 means
     *            10%)
     * @return void
     * */
    public void raiseSalary(double percent) {
        salary *= (1 + percent);
    }

    // 定义变量
    private String name = "";// 实例域初始化
    private double salary;
    private int id;
    private static int nextId = 1;
    private double bonus;

    // 静态初始化块，仅在类第一次加载的时候执行
    static {
        System.out.println("static block : ");
        Random ran = new Random();
        nextId = ran.nextInt(100);
    }

    // 对象初始化块，先于构造器执行
    {
        System.out.println("object block : ");
        bonus = 1000 * nextId;
    }
}
